package business.global.config;

import BaseCommon.CommLog;
import cenum.BanCityEnum;
import core.db.entity.clarkGame.BanCityBO;
import core.db.entity.clarkGame.BanCityBO;
import core.db.other.Restrictions;
import core.db.service.clarkGame.BanCityBOService;
import core.ioc.ContainerMgr;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Data
public class BanCityMgr {

    /**
     * 1、禁止这个城市登录
     * 2、禁止这个城市游戏
     * 3、禁止这个赛事游戏
     * 3、禁止这个赛事登录
     */

    private static class SingletonHolder {
        public static BanCityMgr instance = new BanCityMgr();
    }

    public static BanCityMgr getInstance() {
        return SingletonHolder.instance;
    }

    private Map<String, BanCityBO> banCityMap = new ConcurrentHashMap<>();

    /**
     * 初始化配置
     */
    public void init() {
        CommLog.info("[BanCityMgr.init] load banCity begin...");
        List<BanCityBO> banCityBOList = ContainerMgr.get().getComponent(BanCityBOService.class).findAll(null);
        if (CollectionUtils.isEmpty(banCityBOList)) {
            return;
        }
        for (BanCityBO banCityBO : banCityBOList) {
            this.banCityMap.put(this.getKey(banCityBO.getValue(),banCityBO.getType()), banCityBO);
        }
        CommLog.info("[BanCityMgr.init] load banCity success");
    }

    /**
     * 获取key
     * @param value 值
     * @param type 类型（城市、赛事 ）
     * @return
     */
    private String getKey(int value,int type) {
        return String.format("%d:%d",value,type);
    }

    /**
     * 禁止游戏或者登录
     * @param value 值
     * @param type 类型（城市、赛事 ）
     * @param compareValue （游戏、登录）
     * @return
     */
    public boolean banCity(int value,int type, int compareValue) {
        String key = this.getKey(value,type);
        BanCityBO banType = this.banCityMap.get(key);
        return Objects.nonNull(banType) && (banType.getBanType() > 0 && banType.getBanType() <= compareValue);
    }


    /**
     * 添加
     * @param value 值
     * @param type 类型（城市、赛事 ）
     * @param banType （游戏、登录）
     * @return
     */
    public boolean put(int value,int type,int banType) {
        // 获取key
        String key = this.getKey(value,type);
        BanCityBO banCityBO = this.banCityMap.get(key);
        if (Objects.isNull(banCityBO)) {
            banCityBO = new BanCityBO(value,type,banType);
            if (banCityBO.getBaseService().saveIgnore(banCityBO) <= 0L) {
                CommLog.error("put saveIgnore key:{},value:{},type:{},banType:{}",key,value,type,banType);
                return false;
            }
        } else {
            banCityBO.setValue(value);
            banCityBO.setType(type);
            banCityBO.setBanType(banType);
            if (banCityBO.getBaseService().update(banCityBO) <= 0) {
                CommLog.error("put update key:{},value:{},type:{},banType:{}",key,value,type,banType);
                return false;
            }
        }
        this.banCityMap.put(key,banCityBO);
        return true;
    }

    /**
     * 删除
     * @param value 值
     * @param type 类型（城市、赛事 ）
     * @return
     */
    public boolean delete(int value,int type) {
        ContainerMgr.get().getComponent(BanCityBOService.class).delete(Restrictions.and(Restrictions.eq("value",value),Restrictions.eq("type",type)));
        this.banCityMap.remove(this.getKey(value,type));
        return true;
    }



}
